how to create a database in python using sql lite 3 by by stuart hall

how to create a database in python using sql lite 3 by by stuart hall

Author:by stuart hall [hall, by stuart]
Language: eng
Format: azw3
Published: 2015-04-04T04:00:00+00:00


Please select an option: 1 - Add a new film. 2 - Delete a film. 3 - Show all films. 4 - Sort films by year. 5 - Find films by year. 6 - Find films by genre. q - Exit. Your choice:

Part 5 –the first function def getNum

The first function we are defining validates the integer (or input the user is going to make)

Notice that the end user must type in a whole number as specified

While loop is used so that if the end user enters wrong data the loop will take them back to start

So if a whole number is not entered a ValueError will be generated- it will loop to start and not allow the end user to go any further

Part 6- the second function- def makeFilmTable

This is the sql element the cursor is essential to navigate around the database

The function creates a structure of a database which the information will be stored in

Db.commit() saves the data to the database that is entered in our program by our end user

The sql needs to be put this inside a cursor.execute() statement for it to be carried out by Python

Part 7- define functions to find year to find genre

Notice that the function for Find is created after these two functions that refer to it .This function deals with creating a capacity for end user to enter a number that relates to a selection in the dictionary previously created .getNum is the validation while loop function we put in at start

Again refers to Find function we are going to create in a minute , this time it enables an end user to enter a genre for the film data that they wish to store

Part 8-define the find function

The find function is a helper function for the find functions

Notice that it uses db.commit() to save data to our database we created earlier, and that it has features discussed previously such as cursor =db.cursor()

Db.commit saves all changes made

The ? Is used to indicate that this is data we do not know yet

Cursor.execute -This is used to pass all sql statements to the database

with connect("films.db") as db: forms the connection with the database

Cursor =db.cursor() identifies the cursor for the database ,which tells Python where in the database you are looking at the time

Part 8-define the formatting of the print

The first line sets up the headers for the table.

The next puts in 45 equals signs to go below the header.

Then we use join() to go through each record in results and format it with the same spacing's we used for the headers.

This deals with setting the amount of characters that can be entered within each field type of the database

Ie id- would have a max of 3 characters-title –would be less than 15

Part 9-define delete film function

Db.commit saves all changes made

The ? Is used to indicate that this is data we do not know yet Cursor.execute -This is used to pass all sql statements to the database

with connect("films.db") as db: forms the connection with the database

Cursor =db.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.